using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;
using TimeWar;
using TimeWar.Space;

namespace TimeWar.StarTrek
{
    public class Starship : CRenderedObject
    {
        // Set the 3D model to draw.
        //protected ThreeDimensionalObject shape = null;
        protected Cue engineSound = null;
        protected GameScreen screen = null;
        protected List<Weaponry> weapons = null;
        protected int currWeaponIndex = 0;
        protected PlayerIndex player;
        protected float leftVibe = 1f;
        protected float rightVibe = 1f;
        //protected int spanOfVibration = 1;

        public PlayerIndex Player
        {
            get
            {
                return player;
            }
            set
            {
                player = value;
            }
        }


        public Starship(Game game, String modelLocn, GameScreen currScreen, Int32 objId)
            : base(game, ObjectTypes.Starship, objId)
        {
            // TODO: Construct any child components here
            screen = currScreen;
            //shape = CM.loadModel(modelLocn);
            ModelLocation = modelLocn;
            LoadContent();
            weapons = new List<Weaponry>();
            Initialize();
        }

        public override void Create()
        {
            //throw new Exception("The method or operation is not implemented.");
        }



        protected void playMotionSound()
        {
            Sound.setEmitterPosition(position);

            if (engineSound == null)
            {
                engineSound = Sound.Play(Sounds.ThrustPlayer1, position);
            }

        }

        protected void stopMotionSound()
        {
            if (engineSound != null)
            {
                Sound.Stop(engineSound);
                engineSound.Dispose();
                engineSound = null;
            }
        }

        public void fireWeapon(int index, TimeSpan time)
        {
            Weaponry munitions = null;

            //if (weapons.Count <= index)
            //{
            //    //munition = new Weapon(gameObj, player, position, direction, direction, time, null, "Models\\mgun_proj");
            //    munitions = new Weaponry(GameInstance);
            //}
            //else
            //{
            //    munitions = weapons[index];
            //}

            //Particles particles = new Particles(gameObj);
            //Weapon photonTorp = new Missile(GameInstance, player, position, direction, direction, time, "Content\\Models\\mgun_proj");
            //photonTorp.Scale = 0.01f;
            //screen.OnscreenWeapons.Add(photonTorp);

            //screen.addScreenObject(photonTorp);

            //Projectile bullet = new Projectile(gameObj, player, getWeaponPosition(), Vector3.Multiply(direction, velocity), direction, time, particles);

            //weapons[index].Add();
        }

        protected void explosionSound()
        {
            //currCue = Sound.Play(Sounds.ExplodeShip);
        }

        public override GameState Update(TimeSpan time, TimeSpan elapsedTime)
        {
            //if (true == chased)
           // {
                processGamePadCommands(elapsedTime);
            //}

            Move(elapsedTime);

            if (weapons != null && weapons.Count > 0)
            {
                foreach (Weaponry munitions in weapons)
                {
                    if (munitions.Ammunition.Count > 0)
                    {
                        foreach (Weapon munition in munitions.Ammunition)
                        {
                            munition.Update(time, elapsedTime);
                        }
                    }
                }
            }
            return base.Update(time, elapsedTime);
        }

        public override void Render()
        {
            base.Render();
        }


        public override void onCollision()
        {
            //XInputHelper.GamePads[player].vibrate(leftVibe, rightVibe);
            //for (int i = 0; i < spanOfVibration; i++) ;
            //XInputHelper.GamePads[player].vibrate(0, 0);
        }

        protected void processGamePadCommands(TimeSpan time)
        {

            stopAccelerating();

            if (XInputHelper.GamePads[player].State.IsButtonDown(Buttons.LeftStick))
            {
                //velocity *= 1.05f;
                //MoveForwards(velocity);
                accelerate();
            }
            else if (XInputHelper.GamePads[player].State.IsButtonDown(Buttons.RightStick))
            {
                //velocity *= 0.8f;
                //MoveBackwards(velocity);
                deccelerate();
            }
            else if (XInputHelper.GamePads[player].State.IsButtonUp(Buttons.LeftStick))
            {
                stopAccelerating();
            }
            else if (XInputHelper.GamePads[player].State.IsButtonUp(Buttons.RightStick))
            {
                //stopAccelerating();
            }


            if (XInputHelper.GamePads[player].LeftPressed)
            {
                RollLeft(1);


            }
            else if (XInputHelper.GamePads[player].RightPressed)
            {
                RollRight(1);
            }
            else if (XInputHelper.GamePads[player].UpPressed)
            {
                //PitchDown(1);
            }
            else if (XInputHelper.GamePads[player].DownPressed)
            {
                //PitchUp(1);
            }

            if (XInputHelper.GamePads[player].ThumbStickLeftY < 0)
            {
                //MoveForwards(velocity);
                //PitchDown(1);

            }
            else if (XInputHelper.GamePads[player].ThumbStickLeftY > 0)
            {
                //MoveBackwards(velocity);
                //PitchUp(1);
            }

            ChangeDirection(XInputHelper.GamePads[player].ThumbStickLeftX, XInputHelper.GamePads[player].ThumbStickLeftY);

            if (XInputHelper.GamePads[player].ThumbStickLeftX < 0)
            {
                //StrafeLeft(velocity);
            }
            else if (XInputHelper.GamePads[player].ThumbStickLeftX > 0)
            {
                //StrafeRight(velocity);
            }

            if (XInputHelper.GamePads[player].State.IsButtonDown(Buttons.LeftShoulder))
            {
                RotateLeft(1);
            }
            else if (XInputHelper.GamePads[player].State.IsButtonDown(Buttons.RightShoulder))
            {
                RotateRight(1);
            }

            if (XInputHelper.GamePads[player].State.IsButtonDown(Buttons.LeftTrigger))
            {
                //Cycle between weapons
                currWeaponIndex++;
                if (currWeaponIndex >= weapons.Count)
                {
                    currWeaponIndex = 0;
                }
            }
            else if (XInputHelper.GamePads[player].State.IsButtonDown(Buttons.RightTrigger))
            {
                //Fire current weapon index
                fireWeapon(currWeaponIndex, time);
            }


        }




    }

}
